package com.li.jiang.controller;


import com.li.jiang.obj.dto.PageDTO;
import com.li.jiang.obj.dto.PostDTO;
import com.li.jiang.obj.pojo.Post;
import com.li.jiang.obj.vo.PostVO;
import com.li.jiang.service.impl.PostServiceImpl;
import com.li.jiang.utils.ReturnMessage;
import com.li.jiang.utils.query.PostQuery;
import com.li.jiang.utils.role.RequireRoles;
import com.li.jiang.utils.role.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 岗位表 前端控制器
 * </p>
 *
 * @author 李江
 * @since 2024-04-12
 */
@RestController
@RequestMapping("/post")
public class PostController {

    @Autowired
    private PostServiceImpl service;
    @RequireRoles(role = {Role.ADMIN,Role.COMPANY})
    @PostMapping("/addPost")
    public ReturnMessage addPost(PostDTO postDTO){
        System.out.println("添加岗位："+postDTO);

        if (postDTO==null||!service.addPost(postDTO)){
            return ReturnMessage.error("添加失败，数据有误!");
        }
        return ReturnMessage.success("添加岗位成功");
    }
    @RequireRoles(role = {Role.ADMIN,Role.COMPANY})
    @GetMapping("/delete/{id}")
    public ReturnMessage deletePostById(@PathVariable Long id){
        if (id==null||!service.removeById(id)){
            return ReturnMessage.error("删除岗位失败！");
        }
        return ReturnMessage.success("删除岗位成功!");
    }

    @PostMapping("/updatePost")
    public ReturnMessage updatePostById(PostDTO postDTO){
        System.out.println("修改岗位："+postDTO);
        if (postDTO==null||!service.updatePost(postDTO)){
            return ReturnMessage.error("添加失败，数据有误!");
        }
        return ReturnMessage.success("添加岗位成功");
    }

    @PostMapping("/queryPost")
    public ReturnMessage queryPost(PostQuery postQuery){
        System.out.println("查询岗位信息:"+postQuery);
        System.out.println("当前页数:"+postQuery.getPageNo());
        PageDTO<PostVO> posts = service.queryPost(postQuery);
        return ReturnMessage.success("岗位获取成功！！",posts);
    }
}
